package com.dfrz.planeclub;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.util.HashMap;
import java.util.Map;

public class GaoDeMapWeatherApiExample {
    public static void main(String[] args) {
        // 高德地图API密钥
        String key = "9e273f8b9cf6e52374a97aec79c87de9";
        // 城市名
        String city = "110101";
        // 构建请求URL
        String url = "https://restapi.amap.com/v3/weather/weatherInfo?key=" + key + "&city=" + city ;
        System.out.println(url);

        // 创建HttpClient对象
        HttpClient httpClient = HttpClients.createDefault();
 
        // 创建HttpGet对象
        HttpGet httpGet = new HttpGet(url);
 
        try {
            // 发送请求并获取响应
            HttpResponse response = httpClient.execute(httpGet);
 
            // 从响应对象中提取字符串
            String result = EntityUtils.toString(response.getEntity(), "UTF-8");

            JSONObject jsonObject = JSON.parseObject(result);
            // 打印结果
            System.out.println(jsonObject.get("lives"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}